<template>
  <div>
    <el-dialog :close-on-click-modal="false" :visible.sync="visible">
      <el-dialog
        width="40%"
        title="选择属性"
        :visible.sync="innerVisible"
        append-to-body
      >
        <el-table
          :data="dataList"
          border
          v-loading="dataListLoading"
          @selection-change="innerSelectionChangeHandle"
          style="width: 100%"
        >
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="id"
            header-align="center"
            align="center"
            label="属性id"
          ></el-table-column>
          <el-table-column
            prop="name"
            header-align="center"
            align="center"
            label="属性名"
          ></el-table-column>
          <el-table-column
            prop="icon"
            header-align="center"
            align="center"
            label="属性图标"
          ></el-table-column>
          <el-table-column
            prop="valueSelect"
            header-align="center"
            align="center"
            label="可选值列表"
          ></el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button @click="innerVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitAddRealtion"
            >确认新增</el-button
          >
        </div>
      </el-dialog>
      <el-row>
        <el-col :span="24">
          <el-button type="primary" @click="addRelation">新建关联</el-button>
          <el-table
            class="table-container"
            :data="relationAttrs"
            style="width: 100%"
            border
          >
            <el-table-column prop="id" label="#"></el-table-column>
            <el-table-column prop="name" label="属性名"></el-table-column>
            <el-table-column prop="valueSelect" label="可选值">
              <template slot-scope="scope">
                <el-tooltip placement="top">
                  <div slot="content">
                    <span
                      v-for="(i, index) in scope.row.valueSelect.split(';')"
                      :key="index"
                    >
                      {{ i }}
                      <br />
                    </span>
                  </div>
                  <el-tag>{{
                    scope.row.valueSelect.split(";")[0] + " ..."
                  }}</el-tag>
                </el-tooltip>
              </template>
            </el-table-column>
            <el-table-column
              fixed="right"
              header-align="center"
              align="center"
              label="操作"
            >
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="small"
                  @click="relationRemove(scope.row.id)"
                  >移除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import RelationApi from "@/api/product/attr-group-relation";

export default {
  data() {
    //这里存放数据
    return {
      attrGroupId: null,
      visible: false,
      innerVisible: false,
      relationAttrs: [],
      dataList: [],
      dataListLoading: false,
      innerdataListSelections: [],
    };
  },
  //方法集合
  methods: {
    innerSelectionChangeHandle(val) {
      this.innerdataListSelections = val;
    },
    addRelation() {
      this.getDataList();
      this.innerVisible = true;
    },
    //移除关联
    relationRemove(id) {
      RelationApi.removeRelation({
        attrId: id,
        attrGroupId: this.attrGroupId,
      }).then(({ data }) => {
        this.$message({ type: "success", message: "删除成功" });
        this.init(this.attrGroupId);
      });
    },
    submitAddRealtion() {
      this.innerVisible = false;
      if (this.innerdataListSelections.length > 0) {
        let postData = [];
        this.innerdataListSelections.forEach((item) => {
          postData.push({ attrId: item.id, attrGroupId: this.attrGroupId });
        });
        RelationApi.addRelation(postData).then(({ data }) => {
          this.$message({ type: "success", message: "新增关联成功" });
          this.init(this.attrGroupId);
        });
      } else {
      }
    },
    init(id) {
      this.attrGroupId = id;
      this.visible = true;
      RelationApi.getRelation({ groupId: this.attrGroupId }).then(
        ({ data }) => {
          this.relationAttrs = data;
        }
      );
    },
    getDataList() {
      this.dataListLoading = true;
      RelationApi.getNoRelation({ attrGroupId: this.attrGroupId }).then(
        ({ data }) => {
          this.dataList = data;
          this.dataListLoading = false;
        }
      );
    },
  },
};
</script>
<style scoped></style>
